#pragma once

#include <optional>
#include <QString>
#include "../nlohmann/json.hpp"

using json = nlohmann::json;

class AlarmRecord {
public:
    std::optional<long long> alarmId;
    std::optional<int> deviceId;
    std::optional<std::string> timestamp;
    std::optional<std::string> content;
    std::optional<std::string> status;
    std::optional<std::string> note;

    json toJson() const {
        json j;
        j["alarmId"] = alarmId ? json(*alarmId) : json(nullptr);
        j["deviceId"] = deviceId ? json(*deviceId) : json(nullptr);
        j["timestamp"] = timestamp ? json(*timestamp) : json(nullptr);
        j["content"] = content ? json(*content) : json(nullptr);
        j["status"] = status ? json(*status) : json(nullptr);
        j["note"] = note ? json(*note) : json(nullptr);
        return j;
    }

    static AlarmRecord fromJson(const json &j) {
        AlarmRecord r;
        if (j.contains("alarmId") && !j["alarmId"].is_null()) r.alarmId = j["alarmId"].get<long long>();
        if (j.contains("deviceId") && !j["deviceId"].is_null()) r.deviceId = j["deviceId"].get<int>();
        if (j.contains("timestamp") && !j["timestamp"].is_null()) r.timestamp = j["timestamp"].get<std::string>();
        if (j.contains("content") && !j["content"].is_null()) r.content = j["content"].get<std::string>();
        if (j.contains("status") && !j["status"].is_null()) r.status = j["status"].get<std::string>();
        if (j.contains("note") && !j["note"].is_null()) r.note = j["note"].get<std::string>();
        return r;
    }
};

